<?php

/*
 * 在您使用STS SDK前，请仔细阅读RAM使用指南中的角色管理部分，并阅读STS API文档
 *
 */

namespace Platform\Libs;

use Mts\Request\V20140618\QueryMediaListRequest;

include_once PLATFROM_LIB_PATH . 'aliyunMTS/aliyun-php-sdk-core/Config.php';
include_once PLATFROM_LIB_PATH . 'aliyunMTS/aliyun-php-sdk-mts/Mts/Request/V20140618/QueryMediaListByURLRequest.php';
include_once PLATFROM_LIB_PATH . 'aliyunMTS/aliyun-php-sdk-mts/Mts/Request/V20140618/QueryMediaListRequest.php';

class AliMTS {

    public function CreateToken() {
        // 你需要操作的资源所在的region，STS服务目前只有杭州节点可以签发Token，签发出的Token在所有Region都可用
        // 只允许子用户使用角色
        $accessKeyID = 'LTAI51B4U69qnEtQ'; // 替换成真实的id
        $accessKeySecret = 'gduuTkev5vHmEKPfouwUvLm4Cj96VF'; // 替换成真实的secret
        $profile = \DefaultProfile::getProfile('cn-hangzhou', $accessKeyID, $accessKeySecret);
        $client = new \DefaultAcsClient($profile);

        return $client;
    }

    public function queryMediaById($client, $mediaID) {
        $request = new QueryMediaListRequest();
        $request->setAcceptFormat('JSON');
        $request->setMediaIds($mediaID);
        $response = $client->getAcsResponse($request);
        return $response;
    }

    public function printMedia($media) {
        return [
            'title' => $media->{'Title'},
            'description' => $media->{'Description'},
            'tags' => $media->{'Tags'}->{'Tag'}[0],
            'coverURL' => $media->{'CoverURL'},
            'duration' => $media->{'Duration'}
        ];
        if (array_key_exists('Title', $media)) {
            print_r('Title: ' . $media->{'Title'} . "\n");
        }
        if (array_key_exists('Description', $media)) {
            print_r('Description: ' . $media->{'Description'} . "\n");
        }
        if (array_key_exists('Tags', $media)) {
            print_r('Tags: ' . $media->{'Tags'}->{'Tag'}[0] . "\n");
        }
        if (array_key_exists('CoverURL', $media)) {
            print_r('CoverURL: ' . $media->{'CoverURL'} . "\n");
        }
        if (array_key_exists('Duration', $media)) {
            print_r('Duration: ' . $media->{'Duration'} . "\n");
        }
        print_r('Format: ' . $media->{'Format'} . "\n");
        print_r('Resolution: ' . $media->{'Width'} . 'x' . $media->{'Height'} . "\n");
        print_r('FileSize: ' . $media->{'Size'} . "\n");
        print_r('Bitrate: ' . $media->{'Bitrate'} . "\n");
        print_r('FPS: ' . $media->{'Fps'} . "\n");
        print_r('Duration:' . $media->{'Duration'} . "\n");
    }

    public function getMedia($videoUrl) {
        $client = $this->CreateToken();
        $request = new \Mts\Request\V20140618\QueryMediaListByURLRequest();
        $request->setFileURLs($videoUrl);
        $response = $client->getAcsResponse($request);
        return $response;
    }

    public function getFileInfo($videoUrl) {
        $client = $this->CreateToken();
        $mediaInfo = jsonDecode($this->getMedia($videoUrl));
        if (empty($mediaInfo)) {
            E('获取视频信息失败');
        }
        return [
            'coverURL' => $mediaInfo['MediaList']['Media'][0]['CoverURL'],
            'Duration' => $mediaInfo['MediaList']['Media'][0]['Duration'],
            'Size' => $mediaInfo['MediaList']['Media'][0]['Size'],
        ];
    }

}

?>
